package top.jacktgq.web.servlet;
/**
 * @Author CandyWall
 * @Date 2021/6/20--12:33
 * @Description 处理学生信息的servlet
 */

import top.jacktgq.dao.StudentDao;
import top.jacktgq.pojo.Student;
import top.jacktgq.utils.MapperUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;

@WebServlet(name = "CheckStuIdIfExistServlet", value = "/checkStuIdIfExist")
public class CheckStuIdIfExistServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String stu_id = request.getParameter("stu_id");
        System.out.println("CheckStuIdIfExistServlet参数：" + stu_id);
        StudentDao studentDao = new StudentDao();
        Student student = null;
        try {
            student = studentDao.getStudentById(stu_id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        HashMap<String, Object> resultMap = new HashMap<>();
        boolean ifExist = false;
        if (student != null) {
            ifExist = true;
        }
        resultMap.put("ifExist", ifExist);
        response.getWriter().write(MapperUtils.mapToJson(resultMap));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
